//
//  SLInformationAddVC.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/22.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import UIKit
import SwiftyJSON
import MBProgressHUD

class SLInformationAddVC: SLTableVC<Any> {
    /// 添加信息类型
    enum InformationType {
        case identity
        case base
        case job
        case contacts
        case bank
    }
    var addType: InformationType = .identity
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        self.refreshAction()
    }
    override func refreshData(isPullDown: Bool = true) {
        self.tableview.mj_header?.endRefreshing()
        self.tableview.mj_footer?.endRefreshing()
        var url: String = ""
        switch self.addType {
        case .identity: url = ""
            break
        case .base: url = "/satisfies/inflated"
            break
        case .job: url = "/satisfies/punjab"
            break
        case .contacts: url = "/satisfies/whiskers"
            break
        case .bank: url = "/satisfies/ordered"
            break
        default:
            break
        }
        SLHttpTool.AsyncReportData(path: url, params:["sources":self.id],target: self) { responseCall, success, msg, code in
            if success {
                let array = responseCall["wavelets"]["commonalities"].arrayValue
                self.datas = array
                self.tableview.reloadData()
            }else{
                MBProgressHUD.showError(error: msg)
            }
        }
    }
    func setupUI(){
        self.tableview.snp.remakeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(self.navigationView.snp.bottom)
            make.bottom.equalTo(self.nextButton.snp.top)
        }
        self.tableview.bounces = false
        self.tableview.mj_header = nil
        self.tableview.mj_footer = nil

        self.tableview.emptyDataSetSource = nil
        self.tableview.emptyDataSetDelegate = nil
        self.configAutoHeight(estimatedHeight: 88,autoPullDown: true,nibName: "SLInfoAddCell")
        self.tableview.rowHeight = 88

        self.tableview.register(SLInfoAddCell.self, forCellReuseIdentifier: "SLInfoAddCell")
        
        self.nextButton.isHidden = false
    }

    lazy var nextButton:UIButton = {
        let pt = create_button(title: "Next", titleColor: .white,titleFont: SLFontConfig.medium18,bgColor: rgba(hex: "#1608EC"), image: nil, supView: self.view, closure: { make in
            make.bottom.equalTo(-12-DevConfig.safe_bottom)
            make.left.equalTo(47)
            make.right.equalTo(-47)
            make.height.equalTo(44)
        }, selector: (target: self, action: #selector(buttonClick), event:.touchUpInside))
        pt.layer.cornerRadius = 22

        
        return pt
    }()
    @objc func buttonClick(btn: UIButton){
        
        var url = ""
        var argentina = ""
        if self.addType == .identity{
            url = ""
        }else if self.addType == .base{
            url = "/satisfies/vibrissas"
            argentina = "5"
        }else if self.addType == .job{
            url = "/satisfies/jhelum"
            argentina = "6"
        }else if self.addType == .contacts{
            url = ""
        }else if self.addType == .bank{
            url = "/satisfies/unquestionably"
//            argentina = "8"
            //            GFInformationPopup.showAlert(page: self, bank: "") { tag in
            //            }
        }
        var params:[String: String] = [:]
        params["sources"] = self.id
        
        for data in self.datas {
            let json = data as! JSON
            params.updateValue(json["ghosts"].stringValue, forKey: json["sculptor"].stringValue)
        }
        
        let mbp = MBProgressHUD.showMessage(message: "")
        SLHttpTool.AsyncReportData(path: url,params: params, target: self) { responseCall, success, msg, code in
            mbp.hide(animated: true)
            if success {
                self.navigationController?.popViewController(animated: true)
            }else{
                MBProgressHUD.showError(error: msg)
            }
        }
        SLHttpTool.AsyncUploadData(mahathat:argentina, thais: "\(self.startDate)", target: self) { responseCall, success, msg, code in
            
        }
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        return self.datas.count
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SLInfoAddCell", for: indexPath) as! SLInfoAddCell
        var json = self.datas[indexPath.section] as! JSON
        let type = json["discussed"].stringValue
        cell.titleL.text = json["himalayas"].stringValue
        cell.setPlaceholderText(place:json["referring"].stringValue)
        cell.textField.text = json["ghosts"].stringValue
        
        if type == "hellob"{
            cell.tfBlock = { text in
                json["ghosts"] = JSON(text)
                self.datas[indexPath.section] = json
            }
            cell.setType(type: .defaul)
        }else if type == "helloa"{
            cell.setType(type: .select)
            let array = json["cyrus"].arrayValue
            for data in array {
                if data["transfer"].stringValue == json["ghosts"].stringValue{
                    cell.textField.text = data["suthat"].stringValue
                }
            }
        }else{
            cell.setType(type: .select)
        }
        
        let com = json["travelled"].intValue
        if com == 1 {
            cell.textField.keyboardType = .numberPad
        }else{
            cell.textField.keyboardType = .default
        }
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        var json = self.datas[indexPath.section] as! JSON
        
        //        '"discussed":"helloa"',多选
        //        '"discussed":"hellob"',输入
        //        '"discussed":"helloc"',城市
        let type = json["discussed"].stringValue
        if type == "helloa"{
            let arr = json["cyrus"].arrayValue
            PickerTool.showPick(list: arr,title: json["himalayas"].stringValue, page: self) { value in
                let index = value as! Int
                json["ghosts"] = arr[index]["transfer"]
                
                self.datas[indexPath.section] = json
                self.tableview.reloadData()
            }
        }else if type == "helloc"{
            GFCityPicker.showCityPick(page: self) { value in
                json["ghosts"] = JSON(value)
                self.datas[indexPath.section] = json
                self.tableview.reloadData()
            }
        }
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
    }
}
// MARK: UITableViewDelegate,UITableViewDatasource
extension SLInformationAddVC {
    
}
